//package com.laiketui.payment.controller;
//
//import com.alibaba.fastjson.JSONObject;
//import com.google.common.collect.Maps;
//import com.laiketui.common.api.PublicPaymentConfigService;
//import com.laiketui.core.cache.RedisUtil;
//import com.laiketui.core.diyJavaFunction.StringUtils;
//import com.laiketui.core.lktconst.DictionaryConst;
//import com.laiketui.core.lktconst.GloabConst;
//import com.laiketui.core.utils.okhttp.OkHttpUtils;
//import com.laiketui.domain.vo.pay.PaymentVo;
//import com.laiketui.payment.api.WechatPayService;
//import com.laiketui.payment.util.WechatPayUtils;
//import com.laiketui.root.annotation.HttpApiMethod;
//import github.wxpay.sdk.WXPayConstants;
//import github.wxpay.sdk.WXPayUtil;
//import org.apache.commons.collections.MapUtils;
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RestController;
//
//import javax.servlet.http.HttpServletRequest;
//import java.math.BigDecimal;
//import java.net.URLDecoder;
//import java.util.Map;
//
///**
// * @author wangxian
// */
//@RestController
//public class WechatPayV2NotifyController {
//
//    private final Logger logger = LoggerFactory.getLogger(WechatPayV2NotifyController.class);
//
//    @Autowired
//    private WechatPayService wechatPayServiceV2DubboImpl;
//
//    @Autowired
//    private PublicPaymentConfigService publicPaymentConfigService;
//
//    @Autowired
//    RedisUtil redisUtil;
//
//    @Autowired
//    OkHttpUtils okHttpUtils;
//
//    @RequestMapping("/v2/weChatNotify")
//    @HttpApiMethod(urlMapping = {"app.v2.weChatNotify"})
//    public String weChatNotifyV2(HttpServletRequest request) {
//        try {
//            /**微信返回的请求体*/
//            Map params = Maps.newHashMap();
//            String notifyXml = WechatPayUtils.getRequestBody(request);
//            logger.info("微信回调结果：{}", notifyXml);
//            String weChatXml = request.getParameter("weChat_xml");
//            logger.info(weChatXml);
//            if (StringUtils.isEmpty(weChatXml) && !StringUtils.isEmpty(notifyXml)) {
//                weChatXml = notifyXml;
//            }
//            /**
//             * https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_7&index=3
//             */
//            Map<String, String> notifyMap = WXPayUtil.xmlToMap(weChatXml);
//            String returnCode = MapUtils.getString(notifyMap, "return_code");
//            String resultCode = MapUtils.getString(notifyMap, "result_code");
//            String returnMsg = MapUtils.getString(notifyMap, "return_msg");
//            if (WXPayConstants.SUCCESS.equals(returnCode) && WXPayConstants.SUCCESS.equals(resultCode)) {
//                String payNo = MapUtils.getString(notifyMap, "out_trade_no");
//                BigDecimal total = new BigDecimal(MapUtils.getString(notifyMap, "total_fee"));
//                params.put("total", total);
//                params.put("appid", MapUtils.getString(notifyMap, "appid"));
//                params.put("mchID", MapUtils.getString(notifyMap, "mch_id"));
//                params.put("payNo", payNo);
//
//                String openid = MapUtils.getString(notifyMap, "openid");
//                if (openid != null) {
//                    params.put("openId", openid);
//                }
//                String payConfigStr = null;
//
//                //不同类型的订单获取支付信息的方法不一样
//                if (payNo.contains(DictionaryConst.OrdersType.ORDERS_HEADER_DJ) || payNo.contains(DictionaryConst.OrdersType.ORDERS_HEADER_CZ)) {
//                    payConfigStr = publicPaymentConfigService.getPaymentConfigByMemberPayNo(payNo);
//                } else if (payNo.contains(DictionaryConst.OrdersType.ORDERS_HEADER_MCH_PROMISE)) {
//                    payConfigStr = publicPaymentConfigService.getPaymentConfigByMemberPayNo(payNo);
//                } else {
//                    payConfigStr = publicPaymentConfigService.getPaymentConfigByPayNo(payNo);
//                }
//                payConfigStr = URLDecoder.decode(payConfigStr, GloabConst.Chartset.UTF_8);
//                logger.info("payConfigStr:{}", payConfigStr);
//                JSONObject payConfigJson = JSONObject.parseObject(payConfigStr);
//                logger.info("payConfigJson:{}", payConfigJson);
//
//                params.put("type", payConfigJson.getString("payType"));
//                logger.info("type:{}", payConfigJson.getString("payType"));
//                params.put("storeId", payConfigJson.getString("storeId"));
//                logger.info("storeId:{}", payConfigJson.getString("storeId"));
//                JSONObject wechatPayConfigJson = payConfigJson.getJSONObject("payConfig");
//                logger.info("wechatPayConfigJson:{}", payConfigJson.getJSONObject("payConfig"));
//                //不存在的情况直接返回错误
//                if (!wechatPayConfigJson.containsKey("mch_key")) {
//                    logger.info("微信v2回调失败1:{}", payConfigStr);
//                }
//                params.put("key", wechatPayConfigJson.getString("mch_key"));
//                logger.info("key:{}", wechatPayConfigJson.getString("mch_key"));
//                //TODO 证书路径 新增
//                params.put("certPath", wechatPayConfigJson.getString("sslcert_path"));
//                logger.info("certPath:{}", wechatPayConfigJson.getString("sslcert_path"));
//                params.put("notifyData", weChatXml);
//                PaymentVo paymentVo = new PaymentVo();
//                paymentVo.setPayType(payConfigJson.getString("payType"));
//                paymentVo.setStoreId(Integer.valueOf(payConfigJson.getString("storeId")));
//                paymentVo.setsNo(payNo);
//                if (payConfigJson.getString("userId") != null){
//                    paymentVo.setUserId(payConfigJson.getString("userId"));
//                }
//                if (payConfigJson.getString("map") != null){
//                    paymentVo.setParameter(payConfigJson.getString("map"));
//                }
//                if (payConfigJson.getString("real_sno") != null){
//                    paymentVo.setReal_sno(payConfigJson.getString("real_sno"));
//                }
//                if (payConfigJson.getString("storeType") != null){
//                    paymentVo.setStoreType(payConfigJson.getInteger("storeType"));
//                }
//                params.put("paymentVo", paymentVo);
//                Map map = wechatPayServiceV2DubboImpl.payBack(params);
//                logger.info("微信v2回调成功 处理结果：{}", map);
//                return WXPayConstants.SUCCESS;
//            } else {
//                logger.info("微信v2回调失败2:{}", returnMsg);
//                return WXPayConstants.FAIL;
//            }
//        } catch (Exception e) {
//            logger.error("微信v2回调失败", e);
//            return WXPayConstants.FAIL;
//        }
//    }
//
//
//}
